<?php

/**
 * index
 * @author 刘军 
 * @version 1.0
 * @package application
 */
class Member_space extends Front_Controller
{
	public function __construct() {
		parent::__construct();
		$this->layout = 'layouts/member_space';
	}
	
	//首页
	public function index($id){
		
		if(intval($id) <= 0){
			header('location:/');exit;
		}
		$data = array();
		$this->_get_info($data, $id);
		$this->load->view( $this->_site_path.'/member_space/index', $data );
	}
	
	//新闻动态
	public function lists($id){
		
		$search = array();
		$search['orders'] = array('created' => 'desc');
		$search['attributes']['member_id '] = $id;
		//已审核
		$search['attributes']['status'] = 1;
		$data = array();
		$this->load->model('member_article_model');
		
		// 总数与分页
		$total = $this->member_article_model->total( null, $search );
		$data['total'] = $total;
		// 请使用config_item( 'per_page' )获取全局显示条数
		//$per_page = config_item( 'per_page' ) ;
		$per_page = 10;
		$this->load->library( 'pagination' );
		$pagination_config = array(
				'base_url' => base_url($this->_site_path . '/member_space/lists/' . $id),
				'total_rows' => $total,
				'per_page' => $per_page,
				'uri_segment' => 5,
		);
		//分页
		$this->pagination->initialize( $pagination_config );
		$data['pagination'] = $this->pagination->create_links();
		$search['limit'] = array( 'persize' => $per_page, 'offset' => $this->pagination->get_cur_offset() );
		$data['lists'] = $this->member_article_model->get_values('id', 'title', null, $search );
		$data['id'] = $id;
		$this->load->view( $this->_site_path.'/member_space/lists', $data );
	}
	
	public function article($id, $aid){
		
		if(intval($id)<=0 || intval($aid)<=0){
			header('location:/');exit;
		}
		$this->load->model('member_article_model');
		$data['article'] = $this->member_article_model->get_by_pk($aid);
		if(!$data['article']){
			header('location:' . base_url($this->_site_path . '/member_space/index/' . $id));exit;
		}
		$data['id'] = $id;
		$this->load->view( $this->_site_path.'/member_space/article', $data );
	}
	
	//联系我们
	public function concat($id){
		
		$data = array();
		$this->_get_info($data, $id);
		$this->load->view( $this->_site_path.'/member_space/concat', $data );
	}
	
	//获取资料
	public function _get_info(&$data, $id){
		
		$this->load->model('member_company_info_model');
		$this->load->model('member_model');
		$data['info'] = $this->member_company_info_model->get_by_attributes(array('member_id'=>$id));
		if($data['info']){
			//获取行业
			$this->load->model('member_company_industry_model');
			$data['info']['industry'] = $this->member_company_industry_model->get_value_by_pk($data['info']['industry_id'], 'industry_name');
			//获取规模
			if($data['info']['scale_id'] > 0){
				$scales = config_item('company_scale');
				$data['info']['scale'] = $scales[$data['info']['scale_id']];
			}
			//获取省
			if($data['info']['province_id'] > 0){
				$this->load->model('province_model');
				$data['info']['long_address'] = $this->province_model->get_value_by_pk($data['info']['province_id'], 'province_name');
			}
			//获取市
			if($data['info']['city_id'] > 0){
				$this->load->model('city_model');
				$data['info']['long_address'] .= '&nbsp;' . $this->city_model->get_value_by_pk($data['info']['city_id'], 'city_name');
			}
			//获取县
			if($data['info']['county_id'] > 0){
				$this->load->model('county_model');
				$data['info']['long_address'] .= '&nbsp;' . $this->county_model->get_value_by_pk($data['info']['county_id'], 'county_name');
			}
		}
		$data['member'] = $this->member_model->get_by_pk($id);
		if(!$data['member']){
			header('location:/');exit;
		}
		$data['id'] = $id;
	}
	
	public function ajax_left_info($id){
	
		if(intval($id) <= 0){
			echo '';exit;
		}
		$this->load->model('member_model');
		$data['left_info'] = $this->member_model->get_by_pk($id);
		if($data['left_info']){
			//修改访问次数，一小时内最多访问一次
			if(!$_COOKIE['looks']){
				setcookie('looks', 1, time()+3600);
				$data['left_info']['looks']++;
				$this->member_model->update_by_pk(array('looks'=>$data['left_info']['looks']), $id);
			}
		}
		$this->load->view_part( $this->_site_path.'/member_space/ajax_left_info', $data );
	}
	
	public function get_user_name($id){
		
		if(intval($id) <= 0){
			echo '';exit;
		}
		$this->load->model('member_model');
		$user_name = $this->member_model->get_value_by_pk($id, 'user_name');
		echo json_encode(array('user_name'=>($user_name ? $user_name : '')));
	}
	
}

